from kisp import *
import smtplib
from string import Template
import datetime

class Email(KISP):
	Type = "Action"
	Name = "Email"
	Description = "Emails Recipients"
	Arguments = "Enter Recipients Seperated by Semicolons"

	def Start(self,info,recipients):

		try:
			print "Sending Email"
			message = Template('From: $From\r\nTo: $To\r\nSubject: $Subject\r\n\r\n$Text')

			User = ''
			Password = ''
		
			From = User
			To = recipients
			Subject = 'Invigilator Alert: %s - %s @ %s' %( info['tag.title'] , info['poll.title'] , datetime.datetime.utcfromtimestamp(info['event.time']))
			Text = 'Invigilator Alert\n\nAt: %s\nPoll: %s\nTag: %s\n%s' % (  datetime.datetime.utcfromtimestamp(info['event.time']), info['poll.title'] , info['tag.title'] , info['tag.description'] )

			mailServer = smtplib.SMTP('smtp.gmail.com', 587)
			mailServer.ehlo()
			mailServer.starttls()
			mailServer.ehlo()
			mailServer.login(User, Password)
			mailServer.sendmail( From, To , message.substitute(From=From, To=To, Subject=Subject, Text=Text) )
			mailServer.close()
			print "Sent"
			return 0
		except:
			print "Failed"
			return 1
